package com.springmvc.demo.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.springmvc.demo.mapper.FamilyMemberMapper;
import com.springmvc.demo.mapper.FamilyProfileMapper;
import com.springmvc.demo.mapper.RegNCMSMapper;
import com.springmvc.demo.model.FamilyMember;
import com.springmvc.demo.model.FamilyProfile;
import com.springmvc.demo.model.RegistryFormOfNcms;
import com.springmvc.demo.service.ProfileService;
@Service
@Transactional
public class ProfileServiceImpl implements ProfileService{
	@Autowired
	FamilyProfileMapper familyProfileMapper;
	
	@Autowired
	FamilyMemberMapper familyMemberMapper;
	
	@Autowired
	RegNCMSMapper regNCMSMapper;
	
	@Override
	public List<FamilyProfile> getProfile() {
		// TODO Auto-generated method stub
		
		return familyProfileMapper.selectList(null);
	}

	@Override
	public String addProfile(FamilyProfile familyProfile) {
		// TODO Auto-generated method stub
		int i=familyProfileMapper.insert(familyProfile);
		if(i>0)
			return "成功";
		else
			return "失败";
				
	}

	@Override
	public String updateProfile(FamilyProfile familyProfile) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public FamilyProfile getAFamilyProfile(int id) {
		// TODO Auto-generated method stub
		return familyProfileMapper.getFamily(id);
	}

	@Override
	public List<FamilyMember> getMemberListByFamilyNo(String familyNo) {
		// TODO Auto-generated method stub
		Map<String,Object> m=new HashMap<String,Object>();
		m.put("family_no", familyNo);
		return familyMemberMapper.selectByMap(m);
	}

	@Override
	public String regNCMS(RegistryFormOfNcms r) {
		// TODO Auto-generated method stub
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("id_card", r.getIdCard());
		System.out.println(r.getIdCard());
		List<?>list=regNCMSMapper.selectByMap(map);
		if(list.size()>0) {
			return "已参合";
		}else {
			int i=regNCMSMapper.insert(r);
			
			if(i>0)
				return "参合成功";
			else
				return "参合失败";
		}
		
		

	}

	@Override
	public FamilyMember getMemberInfo(int id) {
		// TODO Auto-generated method stub
		return familyMemberMapper.selectById(id);
	}

	@Override
	public String generateRegistryNo(String familyNo) {
		// TODO Auto-generated method stub
		
		
		return null;
	}

	@Override
	public String updateMember(FamilyMember fmMember) {
		// TODO Auto-generated method stub
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("id_card", fmMember.getIdCard());
		
		List<FamilyMember> list=familyMemberMapper.selectByMap(map);
		FamilyMember fm=list.get(0);
		fm.setIsReg(fmMember.getIsReg());
		familyMemberMapper.updateById(fm);
		return null;
	}

	@Override
	public String addFamilyMember(FamilyMember familyMember) {
		// TODO Auto-generated method stub
		
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("id_card", familyMember.getIdCard());
		List<FamilyMember> list=familyMemberMapper.selectByMap(map);
		if(list.size()>0)
			return "该成员信息已存在";
		
		
		int i=familyMemberMapper.insert(familyMember);
		if(i>0)
			return "添加成功";
		else
			return "添加失败";
	}

}
